<?php


namespace App\controller;


use App\Conf;
use App\controller\choice\Manage;
use App\PackMsg;

class JobRegister extends BaseController
{

    /**
     * returnPackData
     * @return array|string
     * date:2021/12/24
     * time:13:42
     * auth：xyc
     */
    public function returnPackData()
    {
        return $this->choise();
    }


    public function choise()
    {
        if (strpos($this->content, Conf::get('server.delimiter')) !== false) {
            $imgArr = explode(Conf::get('server.delimiter'), $this->content);
            if (count($imgArr) == 2) {
                return $this->pushPrinterJob($imgArr[1], $imgArr[0]);
            }
        }
        throw new \RuntimeException("图片参数错误!");
    }


    /**
     * pushPrinterJob
     * @param $printerName
     * @param $body
     * @return array
     * date:2021/12/24
     * time:13:42
     * auth：xyc
     */
    public function pushPrinterJob($printerName, $body)
    {

        foreach ($this->table as $key => $row) {
            if ($row['printer'] == $printerName) {
                $fd = $key;
            }
        }

        if (isset($fd)) {
            $this->server->send(intval($fd), PackMsg::pack(Manage::TYPE_JOB, $body));
            return [];
        }
        throw new \RuntimeException("发送打印机失败!");
    }
}